<template>
    <div>
        <div v-show="!isShow &&!isProductPlanShow">
            <rui-page :defines="defines">
                <div>
                    <div slot="content">
                        <rui-query :label-width="150"
                                   ref="searchFormManage"
                                   :query="queryData"
                                   :query-rows="queryRows"
                                   :more-rows="moreRows"
                        >
                        </rui-query>
                    </div>
                    <Row>
                        <Alert show-icon>
                            已选择 <span class="select-count">{{selectCount}}</span> 项
                            <a class="select-clear" @click="clearSelectAll">清空</a>
                        </Alert>
                    </Row>
                    <rui-table
                        :loading="loading"
                        :defineId="'defineProductPlanTable'"
                        :fixedLeft="['action']"
                        :fixedRight="[]"
                        :showIndex="false"
                        :select="false"
                        :columns="planColumns"
                        :slots="[{key:'action',slot:'action'},{key:'productName',slot:'productName'}]"
                        @loadDatas="queryData"
                        @on-selection-change="changeSelect"
                        ref-query="searchFormManage"
                        ref="productPlanTable"
                        @on-current-change = "onCurrentChange"
                    >
                        <template slot="toolBar">
                            <Button @click="add" type="primary" icon="md-add">新增</Button>
                            <Button @click="measure" type="primary" icon="md-calculator">测算</Button>
                            <Button @click="updateStatus('5')" type="primary" icon="md-checkmark-circle">子产品上线</Button>
                            <Button @click="updateStatus('2')" type="primary" icon="md-close-circle">子产品生效</Button>
                        </template>
                        <template slot="action" slot-scope="{ row }">
                            <div>
                                <Button @click="view(row)" size="small" type="primary" v-show="row.status!=2&&row.status!=0">查看</Button>
                                <Button @click="edit(row)" size="small" type="primary" v-show="row.status==2||row.status==0">编辑</Button>
                                <Button @click="copy(row)" type="primary">复制</Button>
                            </div>
                        </template>
                        <template slot-scope="{ row, index }" slot="productName">
                            <a @click="view(row)">
                                {{row.productName}}
                            </a>
                        </template>
                    </rui-table>
                </div>
                <Modal
                    v-model="isShowModel"
                    :title="modalTitle"
                    :closable="false"
                    :width="1100"
                    :mask-closable="false"
                >
                    <scheme-selection @next="next" @backPage="backPage" v-if="isShowModel"/>
                    <div slot="footer"></div>
                </Modal>
                <Modal
                    v-model="prototypeModalVisible"
                    :title="modalTitle"
                    :closable="false"
                    :width="1100"
                    :mask-closable="false"
                    @on-ok="copyPrototype"
                >
                    <rui-form :form-options="prototypeFormOptions" :read-only="false" ref="prototypeForm"></rui-form>
                    <div slot="footer">
                        <Button size="small" @click="prototypeModalVisible = false">取消</Button>
                        <Button type="primary" :loading="submitLoading" size="small" @click="copyPrototype">保存</Button>
                    </div>
                </Modal>
            </rui-page>
        </div>
        <productProposalInformation :parentParam="parentParam" @backPage="backPage" v-if="isProductPlanShow"/>
    </div>
</template>
<script>
    import productPlanQueryDefine from "./define/product-plan-query-define";
    import productPlanTableDefine from "./define/product-plan-table-define";

    import schemeSelection from "./schemeSelection.vue";
    import productProposalInformation from "./productProposalInformation.vue";
    import {
        queryProductProposalList,
        copyPrototype,
        updateStatus,
    } from "@/projects/afs-core-business/api/afs-product/product/productPrototype";

    import {getVueInfo} from "@/projects/afs-core-business/api/afs-product/product/productPlan";

    //复制信息
    import {deepClone} from "@/libs/utils/ObjectClone";

    export default {
        name: "product-prototype-manage",
        components: {
            schemeSelection,
            productProposalInformation,
        },
        data() {
            return {
                modalTitle:"选择模板/原型",
                loading: true,
                isShow: false,//整体页面
                isShowModel:false,//选择模板或者原型页面
                isProductPlanShow: false,//方案编辑页面
                parentParam: null,
                selectCount: 0,
                queryRows: [
                    {
                        defineId: "defineProductPlanQuery", span: 8, fields: ["status","productName","productNumber"]
                    },
                ],
                moreRows: [
                    {
                        defineId: "defineProductPlanQuery", span: 8, fields: ["businessType","carType", "carNature"]
                    },
                    {
                        defineId: "defineProductPlanQuery", span: 8, fields: ["interestRateType", "repaymentMethod","discountCategory" ]
                    },
                    {
                        defineId: "defineProductPlanQuery", span: 8, fields: ["additionalLoanOrNot","templateName",'createTimeQuery']
                    },
                ],
                planColumns: [
                    'action', 'productNumber', 'productName', 'templateName', 'createTime', 'updateTime', 'interestRateType', 'repaymentMethod', 'discountCategory',
                    'additionalLoanOrNot','carTypes','carNatures','businessTypes', 'status','loanPeriodMin','loanPeriodMax','downPayRatioMin','repricingCycle','versionNumber'
                ],

                //复制使用
                prototypeModalVisible: false,
                modalTitle: null,
                submitLoading: false,
                prototypeFormOptions: [
                    {
                        grids: [
                            // {
                            //     defineId: "defineProductPlanQuery",
                            //     span: 12,
                            //     fields: ["productNumber"]
                            // },
                            {
                                defineId: "defineProductPlanQuery",
                                span: 12,
                                fields: ["productName"]
                            },
                        ]

                    }
                ],
                //当前选中的数据
                currentRowSelect:null,
            };
        },
        computed: {
            defines() {
                return [
                    {
                        id: "defineProductPlanQuery",
                        fields: productPlanQueryDefine
                    },
                    {
                        id: "defineProductPlanTable",
                        fields: productPlanTableDefine
                    }
                ];
            }
        },
        methods: {
            changeSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            onCurrentChange(currentRow,oldCurrentRow){
                console.info(JSON.stringify(currentRow),JSON.stringify(oldCurrentRow))
                this.currentRowSelect = currentRow;
            },
            clearSelectAll() {
                this.$refs.productPlanTable.getTable().selectAll(false);
            },
            queryData(queryData) {
                // console.log("产品方案查询的原始值"+JSON.stringify(queryData));
                this.loading = true;
                queryProductProposalList(queryData).then(res => {
                    this.loading = false;
                    if (res.code === "0000") {
                        let {records, total} = res.data;
                        this.$refs.productPlanTable.updateTableData(records, total);
                        this.currentRowSelect = null;
                    }
                });
            },
            refresh() {
                this.$refs['productPlanTable'].reloadData();
                this.currentRowSelect = null;
            },
            parentReceive(params) {
                this.isShow = false;
                this.isProductPlanShow = false;
                this.clearSelectAll();
                this.refresh();
            },
            add() {
                this.isShowModel = true;
            },
            measure(){
                let productIdSelect = null;
                let productNameSelect = null;
                if(this.currentRowSelect!=null){
                    productIdSelect = this.currentRowSelect.id;
                    productNameSelect = this.currentRowSelect.productName;
                }
                this.afs.newTab(this,'projects/afs-core-business/pages/product/product-measure/productMeasure','IRR测算','ios-add',{productIdSelect:productIdSelect,productNameSelect:productNameSelect},'measure',[],true)
            },
            next(v) {
                this.isShowModel=false;
                this.isProductPlanShow = true;
                this.isShow = false;
                this.parentParam = v;
                this.parentParam.op = 'add';
            },
            backPage() {
                this.isShowModel = false;
                this.isProductPlanShow = false;
                this.clearSelectAll();
                this.refresh();
            },
            view(v) {
                // console.info("查看的数据："+JSON.stringify(v))
                this.afs.newTab(
                    this,'projects/afs-core-business/pages/product/product-plan/productProposalInformationShow',
                    '产品方案详情查看',
                    'ios-add',
                    {status: '3', productTemplateId: v.productTemplateId, id: v.id, templateCategory: v.templateCategory,productInheritanceType:null,op:'view'},
                    v.id,[],
                    true
                );
            },
            edit(v) {
                this.isProductPlanShow = true;
                this.isShow = false;
                this.parentParam = {
                    status: '2',
                    productTemplateId: v.productTemplateId,
                    id: v.id,
                    templateCategory: v.templateCategory,
                    op:'edit'
                };
            },
            //打开复制页面
            copy(row) {
                this.prototypeModalVisible = true;
                this.$refs.prototypeForm.resetFrom();
                this.$refs.prototypeForm.updateFormData(deepClone(row));
                this.$refs.prototypeForm.setFiledValue('productNumber', null);
                this.$refs.prototypeForm.setFieldRequired('productName', true);
            },
            //开始复制
            copyPrototype() {
                this.submitLoading = true;
                copyPrototype(this.$refs.prototypeForm.getFormData()).then(res => {
                    this.submitLoading = false;
                    if (res.code === "0000") {
                        this.$Message.success("设置成功");
                        this.prototypeModalVisible = false;
                        this.refresh();
                    }
                }).catch(() => {
                    this.submitLoading = false;
                });
            },
            //修改子产品状态
            updateStatus(val){
                // console.info("状态修改"+val);
                // console.info("状态修改"+JSON.stringify(this.currentRowSelect));
                if(this.currentRowSelect!=null){
                    if(this.currentRowSelect.templateCategory==2){
                        this.submitLoading = true;
                        this.currentRowSelect.statusType = val;
                        updateStatus(this.currentRowSelect).then(res => {
                            this.submitLoading = false;
                            if (res.code === "0000") {
                                if('5'==val){
                                    this.$Message.success("设置子产品上线成功");
                                }else if('2'==val){
                                    this.$Message.success("设置子产品生效成功");
                                }
                                this.refresh();
                            }
                        }).catch(() => {
                            this.submitLoading = false;
                        });
                    }else{
                        this.$Message.warning("请先选择子产品！");
                    }
                }else{
                    this.$Message.warning("请先选择子产品！");
                }
            },
        },
        mounted() {
            this.refresh()
        }
    };
</script>
